<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MobileCode extends Model
{
    protected $table = 'mobile_code';

    protected $fillable = [
        'mobile','code','status','send_time','expire_timee','ip'
    ];


    public static function verifyIpCode($mobile,$ip)
    {
        //检测IP 60秒请求内验证码次数达到5次限制请求
        $code = self::where('mobile',$mobile)->where('status',1)
            ->where('ip',$ip)
            ->where('send_time','>',now()->modify('-120 second')->toDateTimeString())
            ->where('send_time','<',now()->toDateTimeString())
            ->count();
        if($code >= 5)
        {
            response()->json(['code'=>400,'message'=>'请求频繁,请稍后在试','status' => 'error'])->setStatusCode(400)->send();
            exit();
        }
        return true;
    }


    public static function verifyCode($mobile,$code)
    {
        $res = self::where('mobile',$mobile)->where('status',1)
            ->where('code', $code)
            ->where('expire_timee','>',now()->toDateTimeString())
            ->first();
        if($res)
        {
            //修改该条状态的状态
            $res->update(['status'=>2]);
            return true;
        }
        return false;
    }
}
